package com.xiyuan.config;

import com.xiyuan.smartutils.Ints;
import com.xiyuan.smartutils.Longs;
import com.xiyuan.smartutils.StrUtils;
import com.xiyuan.smartutils.Validates;
import com.xiyuan.smartutils.constants.SignConstants;
import com.xiyuan.smartutils.constants.SysConstants;
import com.xiyuan.smartutils.xml.Xmlx;

import java.io.Serializable;


/**
 * 工程配置项，每个工程配置组有多个工程配置项<br><br>
 * 每个工程配置项，有key,value,type,desc四个属性<br>
 * key为键<br>
 * value为值<br>
 * type表示该值的类型private|protected|public，分别表示私有不展示给用户看，用户可见，用户可见可修改，默认protected<br>
 * desc表示该配置项的描述信息

 * 1.XML配置格式
 * <item key="name" value="Boot Server" type="protected" desc="系统名称" />
 *  
 * 2.INI配置格式
 * #系统名称
 * name=Boot Server
 * #private版本号,private表示type=private
 * version=v1.0.0
 * 
 * @version v1.0.0 @author lgz 2017-8-27 新建与整理
 */
public class Item implements Serializable, SysConstants, SignConstants
{
    private static final long serialVersionUID = 1L;
    
    // 父配置类
    private Group group;
    
    // 配置参数
    private String key;
    private String value;
    private ItemType type;
    private String desc;
    private char symbol;
    
    public Item(Group group, String key, String value, ItemType type, String desc)
    {
        this(group, key, value, type, desc, '#');
    }
    
    public Item(Group group, String key, String value, ItemType type, String desc, char symbol)
    {
        this.group = group;
        this.key = StrUtils.trim(key);
        this.value = StrUtils.trim(group.isXml() ? Xmlx.toXmlLoad(value) : value);
        this.type = type;
        this.desc = StrUtils.trim(desc);
        this.symbol = symbol;
    }
    
    public Item clone()
    {
        return new Item(group, key, value, type, desc, symbol);
    }
    
    /********************************************************************/
    // 以下为Item运行时支持生成字符串和设置属性
    /********************************************************************/
    
    public String toString()
    {
        if (group.isXml())
        {
            return new StringBuilder("<item")
                    .append(" key=").append(_DOUBLE_QUOTE_).append(key).append(_DOUBLE_QUOTE_)
                    .append(" value=").append(_DOUBLE_QUOTE_).append(Xmlx.toXmlSave(value)).append(_DOUBLE_QUOTE_)
                    .append(" type=").append(_DOUBLE_QUOTE_).append(ItemType.toString(type)).append(_DOUBLE_QUOTE_)
                    .append(" desc=").append(_DOUBLE_QUOTE_).append(desc).append(_DOUBLE_QUOTE_)
                    .append(" />")
                    .toString();
        }
        else
        {
            StringBuilder strb = new StringBuilder();
            if (type != null || desc != null)
            {
                strb.append(symbol);
                if (type != null)
                    strb.append(type);
                if (desc != null)
                    strb.append(desc);
                strb.append(_BR_);
            }
            strb.append(key).append("=").append(value);
            return strb.toString();
        }
    }
    
    /** 设置值 */
    public Item setValue(String value)
    {
        this.value = value;
        return this;
    }
    
    /** 修改对应的组 */
    public Item setGroup(Group group)
    {
        this.group = group;
        return this;
    }
    
    /********************************************************************/
    // 以下为Item属性
    /********************************************************************/
    
    public String getKey()
    {
        return key;
    }
    
    public char getSymbol()
    {
        return symbol;
    }
    
    public ItemType getType()
    {
        return type;
    }
    
    public String getTypeString()
    {
        return ItemType.toString(type);
    }
    
    public boolean isPublic()
    {
        return ItemType.PUBLIC == type;
    }
    
    public boolean isProtected()
    {
        return ItemType.PROTECTED == type;
    }
    
    public boolean isPrivate()
    {
        return ItemType.PRIVATE == type;
    }
    
    public String getDesc()
    {
        return desc;
    }
    
    public String getString()
    {
        return value;
    }
    
    public String getString(String defaultValue)
    {
        return Validates.isBlank(value) ? defaultValue : value;
    }
    
    public String getStringFormat()
    {
        return Xmlx.toXmlSave(value);
    }
    
    public int getInt()
    {
        return Ints.toInt(value);
    }
    
    public int getInt(int defaultValue)
    {
        return Ints.toInt(value, defaultValue);
    }
    
    public long getLong()
    {
        return Longs.toLong(value);
    }
    
    public long getLong(long defaultValue)
    {
        return Longs.toLong(value, defaultValue);
    }
    
    public boolean isTrue()
    {
        return _TRUE_.equalsIgnoreCase(value);
    }
    
    /********************************************************************/
    // 以下定义相等,当group和key都相等时才表示同一个
    /********************************************************************/
    
    @Override
    public int hashCode()
    {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((group == null) ? 0 : group.hashCode());
        result = prime * result + ((key == null) ? 0 : key.hashCode());
        return result;
    }
    
    public boolean equals(Object obj)
    {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Item other = (Item) obj;
        if (group == null)
        {
            if (other.group != null)
                return false;
        }
        else if (!group.equals(other.group))
            return false;
        if (key == null)
        {
            if (other.key != null)
                return false;
        }
        else if (!key.equals(other.key))
            return false;
        return true;
    }
    
}
